package com.qyl.honghengshop.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.PictureCarousel;
import com.qyl.honghengshop.service.PictureService;
import com.qyl.honghengshop.service.impl.PictureServiceImpl;
import com.qyl.honghengshop.utils.FileUpload;
import com.qyl.honghengshop.utils.PageBean;

@Controller
@RequestMapping("/admin/picture")
public class PictureController {

	private PictureService pictureServie = new PictureServiceImpl();
	
	@RequestMapping("/pictureList")
	public String pictureList(Integer pageIndex,Integer pageSize,PictureCarousel picture,ModelMap map){
		
		int recordCount = pictureServie.getRecordCount(picture);
		
		PageBean pageBean = new PageBean(pageIndex, recordCount, pageSize);
		
		pictureServie.pictureList(pageBean,picture);
		
		map.put("pageBean", pageBean);
		
		
		return "picture/pictureList";
	}
	
	@RequestMapping("/toAdd")
	public String toAdd(){
		
		return "picture/pictureAdd";
	}
	
	@RequestMapping("/uploadPicAjax")
	@ResponseBody
	public Map<String, String> uploadPicAjax(MultipartFile picture){
		
		Map<String, String> map = new HashMap<String, String>();
		
		String fileName = null;
		
		try {
			fileName = FileUpload.fileUpload(picture);
			map.put("status", "success");
			map.put("fileName", fileName);
			
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "fail");
		}
		
		return map;
	}
	
	@RequestMapping("/pictureAdd")
	public String pictureAdd(PictureCarousel picture,ModelMap map){
		
		int count = pictureServie.pictureAdd(picture);
		
		if(count > 0){
			map.put(PageConstant.TIP, "保存图片成功");
		}else{
			map.put(PageConstant.TIP, "保存图片失败");			
		}		
		return "picture/pictureAdd";
	}
	
	@RequestMapping("/delPic")
	public String delPic(Integer id,ModelMap map){
		
		int count = pictureServie.delPic(id);
		
		if(count > 0){
			map.put(PageConstant.TIP, "删除图片成功");
		}else{
			map.put(PageConstant.TIP, "删除图片失败");			
		}		
		
		return "forward:pictureList";
	}
	
	@RequestMapping("/toPicUpdate")
	public String toPicUpdate(Integer id,ModelMap map){
		
		PictureCarousel picture = pictureServie.getPicById(id);
		
		map.put("picture", picture);
		
		return "picture/pictureUpdate";
		
	}
	
	@RequestMapping("/pictureUpdate")
	public String pictureUpdate(PictureCarousel picture,ModelMap map){
		
		int count = pictureServie.pictureUpdate(picture);
		
		if(count > 0){
			map.put(PageConstant.TIP, "更改图片成功");
		}else{
			map.put(PageConstant.TIP, "更改图片失败");			
		}		
		return "forward:toPicUpdate";
		
	}
	
	
}
